﻿using DevExpress.XtraBars;
using DevExpress.XtraBars.Docking2010.Views.Widget;
using DevExpress.XtraVerticalGrid;
using System;
using System.Windows.Forms;
using XCI.Sys.Forms;
using XCI.Windows;
using XCI.Windows.Extensions;
using XCI.Windows.Forms;
using XCI.Windows.Helper;

namespace _8.test
{
    public partial class FrmWidgetSetting : FrmBase
    {
        private string code;
        private WidgetManage widgetManage;

        public FrmWidgetSetting(string code)
        {
            InitializeComponent();
            this.code = code;
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        private void InitForm()
        {
            widgetManage = new WidgetManage(widgetView1, code);
            widgetManage.Load();
            SetViewProperty();
            ButtonStatus();
        }

        private void SetViewProperty()
        {
            propertyView.SelectedObject = widgetManage.WidgetViewObject;
            HideRow(propertyView, "Data");
            HideRow(propertyView, "数据");
        }

        private void SetItemProperty()
        {
            propertyView.SelectedObject = widgetManage.WidgetViewObject.ActiveDocument;
            HideRow(propertyView, "Custom Header Buttons");
            HideRow(propertyView, "Deferred Control Load Properties");
        }

        /// <summary>
        /// 设置按钮可用状态
        /// </summary>
        private void ButtonStatus()
        {
            //btnRemove.Enabled = widgetManage.WidgetViewObject.ActiveDocument != null;

            biFlowMode.Down =
                biStackedMode.Down =
                biTableMode.Down =
                biFreeMode.Down = false;
            switch (widgetManage.WidgetViewObject.LayoutMode)
            {
                case LayoutMode.StackLayout:
                    biStackedMode.Down = true;
                    break;
                case LayoutMode.TableLayout:
                    biTableMode.Down = true;
                    break;
                case LayoutMode.FlowLayout:
                    biFlowMode.Down = true;
                    break;
                case LayoutMode.FreeLayout:
                    biFreeMode.Down = true;
                    break;
                default:
                    biFreeMode.Down = true;
                    break;
            }

            biFlowMode.Enabled =
                biStackedMode.Enabled =
                biTableMode.Enabled =
                biFreeMode.Enabled = widgetManage.WidgetData.Count > 0;

            biLeftToRight.Visibility
                = biRightToLeft.Visibility
                = biBottomUp.Visibility
                = biTopDown.Visibility
                = biItemMixer.Visibility = BarItemVisibility.Never;

            if (widgetManage.WidgetViewObject.LayoutMode == LayoutMode.FlowLayout)
            {
                biLeftToRight.Visibility
                = biRightToLeft.Visibility
                = biBottomUp.Visibility
                = biTopDown.Visibility = BarItemVisibility.Always;
            }
            else if (widgetManage.WidgetViewObject.LayoutMode == LayoutMode.TableLayout
                || widgetManage.WidgetViewObject.LayoutMode == LayoutMode.StackLayout)
            {
                biItemMixer.Visibility = widgetManage.WidgetData.Count > 0 ? BarItemVisibility.Always : BarItemVisibility.Never; ;
            }
        }

        private void ApplyLayoutMode(LayoutMode layoutMode)
        {
            widgetManage.WidgetViewObject.BeginUpdateAnimation();
            widgetManage.WidgetViewObject.LayoutMode = layoutMode;
            LayoutMix(layoutMode);
            ButtonStatus();
            widgetManage.WidgetViewObject.EndUpdateAnimation();
        }

        private void HideRow(PropertyGridControl propertyGrid, string caption)
        {
            var row = propertyGrid.GetRowByCaption(caption);
            if (row != null)
            {
                row.Visible = false;
            }
        }

        private void LayoutMix(LayoutMode layoutMode)
        {
            switch (layoutMode)
            {
                case LayoutMode.FlowLayout: widgetManage.FlowLayoutMix(); break;
                case LayoutMode.StackLayout: widgetManage.StackLayoutMix(); break;
                case LayoutMode.TableLayout: widgetManage.TableLayoutMix(); break;
            }
        }

        private void FrmWidgetSetting_Load(object sender, EventArgs e)
        {
            InitForm();
        }

        private void BtnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var editForm = new FrmWidgetItemEdit(widgetManage);
            editForm.ShowDialogOk(() =>
            {
                widgetManage.Save();
                widgetManage.Load();
                ButtonStatus();
            });
        }

        private void BtnRemove_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要删除选中的部件吗?") != DialogResult.Yes)
            {
                return;
            }
            widgetManage.RemoveItem(widgetManage.WidgetViewObject.ActiveDocument.ControlName);
            widgetManage.Save();
            widgetManage.Load();
            ButtonStatus();
        }

        private void BtnOk_Click(object sender, EventArgs e)
        {
            widgetManage.Save();
            ResultOk();
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            ResultCancel();
        }

        private void BtnReset_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要重置吗？重置后配置文件会清空") != DialogResult.Yes)
            {
                return;
            }
            widgetManage.Reset();
            MessageBoxHelper.ShowMessage("重置成功");
        }

        private void BiMode_ItemClick(object sender, ItemClickEventArgs e)
        {
            LayoutMode layoutMode = (LayoutMode)e.Item.Tag;
            ApplyLayoutMode(layoutMode);
        }

        private void BiFlowDirection_ItemClick(object sender, ItemClickEventArgs e)
        {
            widgetManage.WidgetViewObject.BeginUpdateAnimation();
            FlowDirection flowDirection = (FlowDirection)e.Item.Tag;
            widgetManage.WidgetViewObject.FlowLayoutProperties.FlowDirection = flowDirection;
            widgetManage.WidgetViewObject.EndUpdateAnimation();
        }

        private void BiItemMixer_ItemClick(object sender, ItemClickEventArgs e)
        {
            LayoutMix(widgetManage.WidgetViewObject.LayoutMode);
        }

        private void WidgetView1_DocumentActivated(object sender, DevExpress.XtraBars.Docking2010.Views.DocumentEventArgs e)
        {
            if (e.Document != null)
            {
                SetItemProperty();
                ButtonStatus();
            }
        }

        private void BtnViewProperty_ItemClick(object sender, ItemClickEventArgs e)
        {
            SetViewProperty();
        }
    }
}
